﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using RkLib.Configuration;
using System.IO;
namespace CodeCollection
{
    public partial class FormMain : Form
    {
        TvOp tvOp;
        CodeEditor cEditor;
        public FormMain()
        {
            InitializeComponent();
            InitGlobalValue();
#if DEBUG
            Cfg.Initialize(ConfigFileType.INI,gValue.SftCfgBaseDir + "cfg_debug.bin");
#else
            Cfg.Initialize(ConfigFileType.INI, gValue.SftCfgBaseDir + "cfg.bin");
#endif
            }

        protected override void WndProc(ref Message m)
        {
            string str;
            switch (m.Msg)
            {
                case (int)WM_MSG.WM_DISPLAY_CODE:
                    string FilePath = Marshal.PtrToStringAnsi(m.LParam);
                    DisplayCode(FilePath);
                    break;
                case (int)WM_MSG.WM_EDITOR_TEXT_CHANGE:
                    str = this.Text;
                    if (str.Substring(str.Length - 1, 1) != "*")
                    {
                        str += " *";
                        this.Text = str;
                    }
                    break;
                case (int)WM_MSG.WM_EDITOR_SAVE:
                    str = this.Text;
                    if (str.Substring(str.Length - 1, 1) == "*")
                    {
                        str = str.Substring(0,str.Length - 2);
                        this.Text = str;
                    }
                    break;
                case (int) WM_MSG.WM_TV_REFRESH:
                    tvOp.Refresh();
                    break;
                default:
                    break;
            }
            base.WndProc(ref m);
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            LoadCfg();
            if (gValue.SrcCodeDir == "")
            {
                CloseForm();
            }

            cEditor = new CodeEditor(this.textEditor,this.Handle);
            tvOp = new TvOp(this.treeView, this.Handle, gValue.SrcCodeDir);
            tvOp.Refresh();
            this.Icon = CodeCollection.Properties.Resources.ico_main;
            this.Text = gValue.FORM_CAPTION;
            tvOp.DisplayLstFile();
        }

        private void LoadCfg()
        {
            gValue.SrcCodeDir = Cfg.ReadString(gValue.SFT_SEC_CFG, gValue.SFT_SRC_CODE_DIR, "");
            if (gValue.SrcCodeDir == "")
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    gValue.SrcCodeDir = fbd.SelectedPath;
                }
            }
        }
        private void SaveCfg()
        {
            Cfg.WriteConfig(gValue.SFT_SEC_CFG, gValue.SFT_SRC_CODE_DIR, gValue.SrcCodeDir);
        }
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveCfg();
            if (!cEditor.Saved)
            {
                cEditor.SaveToFile();
            }
            Cfg.Terminate();
        }
        private delegate DialogResult dg_ShowMsg(string msg, string caption, MessageBoxButtons buttons);
        private DialogResult ShowMsg(string msg, string caption = "提醒", MessageBoxButtons buttons = MessageBoxButtons.OK)
        {
            if (this.InvokeRequired)
            {
                dg_ShowMsg method = ShowMsg;
                return (DialogResult)this.Invoke(method, msg, caption, buttons);
            }
            else
            {
                return MessageBox.Show(msg, caption, buttons);
            }
        }
        private void DisplayCode(string filePath)
        {
            this.Text = gValue.FORM_CAPTION + filePath;
            cEditor.DisplayCode(filePath);
        }
        private void InitGlobalValue()
        {
            gValue.SftCfgBaseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + gValue.SoftName + "\\";
            if (!Directory.Exists(gValue.SftCfgBaseDir))
            {
                Directory.CreateDirectory(gValue.SftCfgBaseDir);
            }
        }
        private delegate void dg_CloseForm();
        private void CloseForm()
        {
            if (this.InvokeRequired)
            {
                dg_CloseForm method = CloseForm;
                Invoke(method);
            }
            else
            {
                this.Close();
            }
        }
    }
}
